package com.citysky.main.controller;

import com.citysky.blog.domain.ContentDO;
import com.citysky.blog.service.ContentService;
import com.citysky.main.vo.BlogVO;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Ralf
 * @since 2018-11-27 17:23.
 * Copyright (c) 2015-2017 wlqq.com. All Rights Reserved.
 */
@RequestMapping("/mall")
@Controller
public class MallController {

    @Autowired
    ContentService bContentService;

    @GetMapping(path = {"", "mall/index"})
    String mallIndex() {
        return "mall/index";
    }

    @GetMapping("service")
    String mallService(@RequestParam Map<String, String> requestParams, Model model) {

        return "mall/service";
    }

    @GetMapping("blog")
    String mallBlog(@RequestParam Map<String, Object> requestParams, Model model) {

        PageInfo<?> pageInfo = buildBlogVOList(requestParams);
        model.addAttribute("pageInfo", pageInfo);
        return "mall/blog";
    }

    private PageInfo<?> buildBlogVOList(Map<String, Object> requestParams) {
        RowBounds rowBounds = new RowBounds(0, 8);
        List<ContentDO> bContentList = bContentService.queryList(new ContentDO(), rowBounds);

        if (CollectionUtils.isNotEmpty(bContentList)) {
            PageInfo<?> pifo = new PageInfo<>(bContentList);
            List blogList = new ArrayList<>();
            for (ContentDO contentDO : bContentList) {
                BlogVO blog = new BlogVO();
                blog.setAuthorHeadImg("/assets/images/newest-author-01.jpg");
                blog.setAuthorName(contentDO.getAuthor());
                blog.setCreateTime(contentDO.getGtmCreate());
                blog.setImg("/assets/images/blog-feature-01.jpg");
                blog.setId(contentDO.getCid());
                blog.setTabloid("After analyzing hundreds of SERPs, Rand has identified and classified the" +
                        " 10 distinct content types that work best for SEO. In this Whiteboard Friday," +
                        " he explains those types ...");
                blog.setTips(Lists.newArrayList("Tips1", "Tips2"));
                blog.setTitle(contentDO.getTitle());
                blog.setViewCount(contentDO.getHits());
                blogList.add(blog);
            }
            pifo.setList(blogList);
            return pifo;
        }

        return new PageInfo<>(new ArrayList<>());
    }

    @RequestMapping("content/{cid}")
    String blogContent(Model model, @PathVariable Long cid) {

        ContentDO contentDO = bContentService.get(cid);
        model.addAttribute("bContent", contentDO);
        return "mall/content";
    }

    @GetMapping("about")
    String mallAbout() {
        return "mall/about";
    }

    @GetMapping("contactus")
    String mallContact() {
        return "mall/contactus";
    }
}
